<?php
/*
Cogumelo v0.2 - Innoto S.L.
Copyright (C) 2010 Innoto Gestión para el Desarrollo Social S.L. <mapinfo@map-experience.com>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
USA.
*/

abstract class C_RequestManager
{
	public static function Request($uri, $matched)
	{
		// decode GET and POST Data
		global $getData, $postData;
		$getData = self::decodeData($_GET);
		$postData = self::decodeData($_POST);
		
		// pattern command match
		if(preg_match('^(redirect:)(.*)^', $matched, $m))
			self::Redirect($m[2]);
			
		else if(preg_match('^(action:)(.*)^', $matched, $m))
			self::Action($m[2], $uri, $getData, $postData);

		else if(preg_match('^(view:)(.*)^', $matched, $m))
			self::View($m[2], $uri, $getData, $postData);
			
		else if(preg_match('^(module:)(.*)^', $matched, $m))
			self::Module($m[2], $uri);
			
		else if(preg_match('^(script:)(.*)^', $matched, $m))
			self::Script($m[2], $uri, $getData, $postData);
		
		/*else if(preg_match('^(stream:)(.*)^', $matched, $m))
			self::Stream($m[2]);*/
		
		else
			Cogumelo::Error(__METHOD__." error. No valid pattern = '$matched'");

		exit;
	}
	
	public static function Redirect($redirect_url)
	{
		header("HTTP/1.1 301 Moved Permanently");
		header("Location: {$redirect_url}");
		exit;
	}
	
	protected static function Module($module, $uri)
	{
		Cogumelo::LoadModule($module);
		if($module::AccessCheck($uri))
			Cogumelo::Request($uri, $module::$url_patterns);
	}
	
	protected static function Script($file, $_URI, $getData, $postData)
	{
		include $file;
	}
	
	/*protected static function Stream($file)
	{
		header( "Content-Type: ".mime_content_type(SITE_PATH.$file) );
		die( file_get_contents( SITE_PATH.$file ) );
	}*/
	
	protected static function View($file, $_URI, $getData, $postData)
	{
		if( !file_exists(SITE_PATH."Controllers/Views/$file")  )
			Cogumelo::Error(__METHOD__."(): Unable to include file '$file'. Expected location: 'Controllers/Views/'. include_path=".ini_get('include_path') );
		include("Controllers/Views/$file");
	}
	
	protected static function Action($file, $_CMD, $getData, $postData)
	{
		if( !file_exists(SITE_PATH."Controllers/Actions/$file") )
			Cogumelo::Error(__METHOD__."(): Unable to include file '$file'. Expected location: 'Controllers/Actions/'. include_path=".ini_get('include_path') );
		include("Controllers/Actions/$file");
	}
	
	private static function decodeData(array $data)
	{
		// temporal fix about received data decoding
		return $data;
		/*$result = array();
		foreach($data as $key => $value)
		{
			if(is_array($value))
				$result[$key] = self::decodeData($value);
			else
				$result[$key] = urldecode(trim($value));
		}
		return $result;*/
	}
}